package
{
	import com.util.Camera;
	
	import starling.animation.Transitions;
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.MovieClip;
	import starling.events.Event;
	
	public class Bunky extends GameObject implements ICollidable
	{
		private const STATE_RUNNING:uint=0x1;
		private const STATE_JUMPING:uint=0x2;
		private const STATE_DYING:uint=0x4;
		private const STATE_RECOVER:uint=0x8;

		public static const MAX_HEALTH_BUNKY:uint = 3;
		
		private var runAnim:MovieClip;
		private var bunkyHealth:uint;
		
		private var currentState:uint;
		private var delay:Number = 0;
		private var recoveryDelay:int;
		private var levelFinish:Boolean;
		private var _worldHeight:Number;
		private var _worldWidth:Number;
		
		public function Bunky()
		{
			super();						
			
			this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
			//this.addEventListener(starling.events.TouchEvent.TOUCH, onTouched);
		}
		
		private function onAddedToStage(event:Event):void
		{
			this.removeEventListener(starling.events.Event.ADDED_TO_STAGE, null);
			
			runAnim = new MovieClip(Assets.getskeleton().getTextures("bunky/bunki_run"),25);
			this.x = 20;
			this.y = 672;//Starling.current.stage.stageHeight - Starling.current.stage.stageHeight * 0.5;
			bunkyHealth = MAX_HEALTH_BUNKY;
			this.addChild(runAnim);			
			levelFinish = false;
			var world:World = this.parent as World;
			world.heroJuggler.add(runAnim);
			_worldWidth = world.getTMXTileMap().mapWidth()* 32;
			_worldHeight = world.getTMXTileMap().mapHeight()* 32;

			collisionmanager.addCollidable(this);			
		}		
		
		public override function onEnterFrame(dt:Number):void
		{			
			var X:Number = this.x + camera.getCameraPosX();
			var Y:Number = this.y + camera.getCameraPosY();
			
			if(Y > _worldHeight)
			{
				Starling.juggler.delayCall(onBunkyDeath, 1.0);
			}
			else if(_worldWidth - X <= 640)
			{
				if(!levelFinish)
				{
					levelFinish = true;
					dispatchEvent(new BunkyDeathEvent(BunkyDeathEvent.BUNKY_LEVEL_FINISH, true));	
				}				
			}
			else if(X > _worldWidth)
			{
				onBunkyDeath();
			}						
		}	
		
		private function onBunkyDeath():void
		{			
			dispatchEvent(new BunkyDeathEvent(BunkyDeathEvent.BUNKY_DEATH, true));
		}

		public function handleInput(action:String):void
		{
		}

		public function isBunkyJumping():Boolean
		{
			return false;
		}
				
		public function onColliding(tgt:ICollidable):void
		{
			if(tgt is tinku)
			{
				--bunkyHealth;
				dispatchEvent(new BunkyEvent(BunkyEvent.BUNKY_DAMAGE, true)); 
				if(bunkyHealth == 0)
				{
					onBunkyDeath();
				}										
			}			
			else
			{
				dispatchEvent(new BunkyEvent(BunkyEvent.COLLECT_REWARD, true));
			}
		}		

		public function get camera():Camera
		{
			var world:World = this.parent as World;
			return world.camera;
		}

		public function get collisionID():uint
		{
			return collisionmanager.BUNKY;	
		}
		
		public override function dispose():void
		{						
			super.dispose();
			collisionmanager.removeCollidable(this);
			runAnim = null;
		}
		
		public function advanceTime(time:Number):void
		{
				
		}		
	}
}